#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: liang kang
@contact: gangkanli1219@gmail.com
@time: 2018/2/5 16:06
@desc: 
"""
import logging
import os
import sys
from urllib import request

current_path = os.path.dirname(os.path.abspath(__file__))

logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.INFO, stream=sys.stdout)

MASK = os.path.join(current_path, 'data', 'high', 'mask.npy')

MODEL_HIGH_MODEL = os.path.join(current_path, 'data', 'high', '73205' 'saved_model.pb'),
MODEL_HIGH_INDEX = os.path.join(current_path, 'data', 'high', '73205', 'variables', 'variables.index')
MODEL_HIGH_DATA = os.path.join(current_path, 'data', 'high', '73205', 'variables', 'variables.data-00000-of-00001')

MODEL_LOW_CENTER = os.path.join(current_path, 'data', 'low', 'frozen_inference_graph_fast_center_200000.pb')
MODEL_LOW_EDGE = os.path.join(current_path, 'data', 'low', 'frozen_inference_graph_fast_edge_160998.pb')

PATH_LIST = [MASK, MODEL_HIGH_MODEL, MODEL_HIGH_DATA, MODEL_HIGH_INDEX, MODEL_LOW_CENTER, MODEL_LOW_EDGE]
with open('./urls.txt') as file:
    URLS = file.readlines()

for file_path, url in zip(PATH_LIST, URLS):
    logging.info('Fetching {}.'.format(file_path))
    if not os.path.exists(file_path):
        logging.info('File {} is not existed !'.format(file_path))
        file_dir = os.path.dirname(file_path)
        if not os.path.exists(file_dir):
            logging.info('Making dir for {}.'.format(file_dir))
            os.makedirs(file_dir)
        else:
            logging.info('Dir {} is existed !'.format(file_dir))
        logging.info('Downloading {} ......'.format(file_path))
        with request.urlopen(url) as web:
            with open(file_path, 'wb') as outfile:
                outfile.write(web.read())
    else:
        logging.info('File {} exists.'.format(file_path))
